package com.terrynoya.fight.evaluation
{
	import com.terrynoya.coreLib.MHashMap;

	public class EvaluationSystem
	{
		private var _expressonCache:MHashMap;
		
		private var _tokenizer:Tokenizer;
		
		private var _treeBuilder:TreeBuilder;
		
		private var _compiler:Compiler;
		
		public function EvaluationSystem()
		{
			this._expressonCache = new MHashMap();
			this._tokenizer = new Tokenizer();
			this._treeBuilder = new TreeBuilder(this);
			this._compiler = new Compiler();	
		}
		
		public function createExpression(input:String):Node
		{
			var tokens:Array = this._tokenizer.tokenize(input);
			var nodes:Array = this._treeBuilder.buildTree(tokens);
			var rlt:Node = nodes[0];
			return rlt;
		}
		
		public function createFullExpression(input:String):Array
		{
			var tokens:Array = this._tokenizer.tokenize(input);
			var nodes:Array = this._treeBuilder.buildTree(tokens);
			return nodes;
		}
		
		public static function print(arr:Array):void
		{
			for (var i:int = 0; i < arr.length; i++) 
			{
				var node:Node = arr[i];
				printNode(node);
			}
		}
		
		public static function printNode(node:Node,depth:int = 0):void
		{
			if(node == null)
			{
				return;
			}
			trace(getIndent(depth) + node.token.text);
			for (var i:int = 0; i < node.chilren.length; i++) 
			{
				var child:Node = Node(node.chilren[i]);
				printNode(child,depth + 1);
			}
		}
		
		public static function getIndent(depth:int):String
		{
			var tmpStr:String = "--";
			var rlt:String = "";
			for (var i:int = 0; i < depth; i++) 
			{
				rlt += tmpStr;
			}
			return rlt;
		}
	}
}